#!/usr/bin/env python

import socket
import sys
import os.path
import string

def expand_path(in_path):
    if os.path.exists(in_path):
        return os.path.abspath(in_path)
    else:
        return in_path
    
def do_main():
    app = sys.argv[1]
    args = [app] + map(expand_path, sys.argv[2:])
    print 'args =', args
    send_string = string.join(args, '\n') + '\n\n'

    config_file = os.path.join(os.environ.get('HOME', ''),
                               '.app_server',
                               'config')

    import ConfigParser;
    config_parser = ConfigParser.ConfigParser()
    try:
        config_parser.read(config_file)
    except ConfigParser.MissingSectionHeaderError, error:
        sys.stderr.write('Warning - config file has no sections.\n')

    host = config_parser.get('NETWORK', 'host')
    port = config_parser.getint('NETWORK', 'port')

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    s.send(send_string)

    data = s.recv(1024)
    s.close()
    print 'Received', data

if __name__ == '__main__':
    do_main()
